package com.java.member.controller;


import cn.hutool.core.util.StrUtil;
import com.java.common.constants.ResultCodeEnum;
import com.java.common.vo.R;
import com.java.member.entity.MyUser;
import com.java.member.service.MyUserService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jiangli
 * @since 2019-12-09
 */
@RestController
@RequestMapping("/member")
public class MyUserController {
	@Autowired
	private MyUserService myUserService;

	/**
	 * 通过账号密码手机邮箱注册
	 */
	@PostMapping("/registry")
	public R registry(@RequestBody @Valid MyUser myUser) {
		return myUserService.registry(myUser);
	}

	/**
	 * 使用账号密码登录
	 */
	@PostMapping("/login")
	public R login(@RequestBody MyUser myUser) {
		String username = myUser.getUsername();
		String password = myUser.getPassword();
		if (StrUtil.isEmpty(username)) {
			return R.error("用户名为空");
		}
		if (StrUtil.isEmpty(password)) {
			return R.error("密码为空");
		}
		return myUserService.login(username,password);
	}

	/**
	 * 通过token查询用户信息
	 */
	@GetMapping
	public R getUserInfoByToken(@RequestParam("token")String token) {
		if (StrUtil.isEmpty(token)) {
			return R.e(ResultCodeEnum.PARAM_ERROR);
		}
		return myUserService.getUserInfo(token);
	}

	/**
	 * 通过openid登录
	 */
	@GetMapping("/getUserByOpenid")
	public R loginByOpenid(@RequestParam("openid") String openid,String nickname) {
		return myUserService.loginByOpenid(openid,nickname);
	}
}

